import router from "@/router";
import {getToken} from "@/utils/auth";
import {hideFullLoading, showFullLoading, toast} from "@/utils/util";
//全局前置守卫
router.beforeEach((to, from, next) => {
    // 显示loading
    showFullLoading()

    const token = getToken();

    //未登录
    if (!token && to.path !== '/login') {
        toast('请先登录', 'error');
        return next({path: '/login'});
    }

    //已登录
    if (token && to.path === '/login') {
        toast('您已登录', 'error')
        return next({path: from.path ? from.path : '/'});
    }

    //登陆则自动获取用户信息

    //动态标题
    document.title = (to.meta.title ? to.meta.title : '后台首页') + ' - ' + "农业监控系统"
    //放行
    next();
});

// 全局后置守卫
router.afterEach((to, from) => hideFullLoading())
